package com.hh.service.service.impl.pagemanagement;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hh.common.enums.ProductRecommend;
import com.hh.common.exception.HhException;
import com.hh.common.model.product.StoreProduct;
import com.hh.common.page.CommonPage;
import com.hh.common.request.PageParamRequest;
import com.hh.common.response.IndexProductResponse;
import com.hh.common.response.pagemanagement.HomeRecommendResponse;
import com.hh.common.response.pagemanagement.ProductResponse;
import com.hh.service.dao.StoreProductDao;
import com.hh.service.service.pagemanagement.ProductRecommendService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductRecommendServiceImpl implements ProductRecommendService {

    @Resource
    private StoreProductDao storeProductDao;

    @Override
    public Map<String, HomeRecommendResponse> getRecommend() {
        Map<String, HomeRecommendResponse> map = new HashMap<>();
        HomeRecommendResponse homeRecommendResponse01 = new HomeRecommendResponse();

        homeRecommendResponse01.setRecommendType(ProductRecommend.SUPERIORPRODUCTSRECOMMEND.getType());
        homeRecommendResponse01.setRecommendTypeName(ProductRecommend.SUPERIORPRODUCTSRECOMMEND.getInfo());
        List<ProductResponse> list01 = this.getRecommendByType(ProductRecommend.SUPERIORPRODUCTSRECOMMEND.getType());
        homeRecommendResponse01.setProduct(list01);
        map.put(ProductRecommend.SUPERIORPRODUCTSRECOMMEND.getType(), homeRecommendResponse01);

        HomeRecommendResponse homeRecommendResponse02 = new HomeRecommendResponse();
        homeRecommendResponse02.setRecommendType(ProductRecommend.RECOMMENDEDFORYOU.getType());
        homeRecommendResponse02.setRecommendTypeName(ProductRecommend.RECOMMENDEDFORYOU.getInfo());
        List<ProductResponse> list02 = this.getRecommendByType(ProductRecommend.RECOMMENDEDFORYOU.getType());
        homeRecommendResponse02.setProduct(list02);
        map.put(ProductRecommend.RECOMMENDEDFORYOU.getType(), homeRecommendResponse02);

        HomeRecommendResponse homeRecommendResponse03 = new HomeRecommendResponse();
        homeRecommendResponse03.setRecommendType(ProductRecommend.HOTPRODUCTS.getType());
        homeRecommendResponse03.setRecommendTypeName(ProductRecommend.HOTPRODUCTS.getInfo());
        List<ProductResponse> list03 = this.getRecommendByType(ProductRecommend.HOTPRODUCTS.getType());
        homeRecommendResponse03.setProduct(list03);
        map.put(ProductRecommend.HOTPRODUCTS.getType(), homeRecommendResponse03);

        HomeRecommendResponse homeRecommendResponse04 = new HomeRecommendResponse();
        homeRecommendResponse04.setRecommendType(ProductRecommend.SPECIALOFFERZONE.getType());
        homeRecommendResponse04.setRecommendTypeName(ProductRecommend.SPECIALOFFERZONE.getInfo());
        List<ProductResponse> list04 = this.getRecommendByType(ProductRecommend.SPECIALOFFERZONE.getType());
        homeRecommendResponse04.setProduct(list04);
        map.put(ProductRecommend.SPECIALOFFERZONE.getType(), homeRecommendResponse04);

        return map;
    }


    @Override
    public PageInfo<IndexProductResponse> getRecommendList(String type, Integer sortType,PageParamRequest pageParamRequest) {
        this.checkRecommendType(type);
        Page<StoreProduct> storeProductPage = PageHelper.startPage(pageParamRequest.getPage(),
                pageParamRequest.getLimit());

        if (ProductRecommend.SUPERIORPRODUCTSRECOMMEND.getType().equals(type)) {
            List<IndexProductResponse> storeProductList = this.storeProductDao.getRecommendSuperiorproductsrecommendListPage();
            if (CollUtil.isEmpty(storeProductList)) {
                return CommonPage.copyPageInfo(storeProductPage, CollUtil.newArrayList());
            }
            return CommonPage.copyPageInfo(storeProductPage, storeProductList);
        } else if (ProductRecommend.RECOMMENDEDFORYOU.getType().equals(type)) {

            List<IndexProductResponse> storeProductList = this.storeProductDao.getRecommendforyouListPage();
            if (CollUtil.isEmpty(storeProductList)) {
                return CommonPage.copyPageInfo(storeProductPage, CollUtil.newArrayList());
            }
            return CommonPage.copyPageInfo(storeProductPage, storeProductList);

        } else if (ProductRecommend.HOTPRODUCTS.getType().equals(type)) {
            List<IndexProductResponse> storeProductList = this.storeProductDao.getHotproductsListPage();
            if (CollUtil.isEmpty(storeProductList)) {
                return CommonPage.copyPageInfo(storeProductPage, CollUtil.newArrayList());
            }
            return CommonPage.copyPageInfo(storeProductPage, storeProductList);

        } else {
            List<IndexProductResponse> storeProductList = this.storeProductDao.getRecommendSpecialofferZoneListPage();
            if (CollUtil.isEmpty(storeProductList)) {
                return CommonPage.copyPageInfo(storeProductPage, CollUtil.newArrayList());
            }
            return CommonPage.copyPageInfo(storeProductPage, storeProductList);
        }
    }

    private void checkRecommendType(String type) {
        ProductRecommend productRecommend = ProductRecommend.getByType(type);
        if (productRecommend == ProductRecommend.UNKNOWN) {
            throw new HhException("类型错误");
        }
    }

    private List<ProductResponse> getRecommendByType(String type) {

        ProductRecommend productRecommend = ProductRecommend.getByType(type);

        List<ProductResponse> productResponses = new ArrayList<>();

        switch (productRecommend) {
            case SUPERIORPRODUCTSRECOMMEND:
                productResponses = this.storeProductDao.selectSuperiorProductsRecommendList();
                break;
            case RECOMMENDEDFORYOU:
                productResponses = this.storeProductDao.selectRecommendedForYouList();
                break;
            case HOTPRODUCTS:
                productResponses = this.storeProductDao.selectHotProductsList();
                break;
            case SPECIALOFFERZONE:
                productResponses = this.storeProductDao.selectSpecialOfferZoneList();
                break;


        }
        return productResponses;
    }
}
